package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.palace.CrossPalaceActivity;
import com.yanqu.road.server.manager.palace.CrossPalaceActivityMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C_PALACE_ASSISTANT_ACTION, desc = "皇城跨服 小助理操作")
public class CrossPalaceWorshipAssistantActionCmd extends CrossCommand {

    private long userId;
    private int activityId;
    private AssistantProto.AssistantCrossActionReqMsg crossMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        crossMsg = AssistantProto.AssistantCrossActionReqMsg.parseFrom(packet.getBytes());
        activityId = crossMsg.getActivityId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceWorshipRespMsg.Builder respMsg = EstateWarProto.PalaceWorshipRespMsg.newBuilder();
        CrossPalaceActivity crossPalaceActivity = CrossPalaceActivityMgr.getCrossPalaceActivity(activityId);
        if (null == crossPalaceActivity) {
            respMsg.setRet(GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND);
        } else {
            respMsg = crossPalaceActivity.worship();
        }
        AssistantProto.AssistantActionRespMsg.Builder assistantActionRespMsg = crossMsg.getAssistantActionRespMsg().toBuilder();
        assistantActionRespMsg.setPalaceWorshipRespMsg(respMsg);
        AssistantProto.AssistantCrossActionRespMsg.Builder builder = AssistantProto.AssistantCrossActionRespMsg.newBuilder();
        builder.setAssistantActionReqMsg(crossMsg.getAssistantActionReqMsg());
        builder.setAssistantActionRespMsg(assistantActionRespMsg);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_ASSISTANT_PALACE_WORSHIP_ACTION_FROM_CROSS, builder);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {
    }
}
